<?php
declare(strict_types=1);

namespace App\Controller\v1;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Qbhy\HyperfAuth\Annotation\Auth;
use Qbhy\HyperfAuth\AuthManager;
use App\Helper\Common;
use Throwable;
use App\Service\LoginService;
use App\Service\StatisrecService;
use App\Service\CopyrightService;
use Overtrue\Flysystem\Qiniu\QiniuAdapter;
use App\Middleware\Api\AuthMiddleware;

#[Controller]
class CommonController extends AbstractController
{
    #[Inject]
    protected LoginService $loginServ;
    #[Inject]
    protected AuthManager $auth;
    #[Inject]
    protected StatisrecService $staServ;
    #[Inject]
    protected CopyrightService $cpyServ;
    
    #用户登录
    #[RequestMapping(path: "login", methods: "post")]
    public function login(){
        $type = $this->request->input("type", NULL);
        $device = $this->request->input("device", NULL);
        $version = $this->request->input("version", NULL);
        if(empty($type)){
            return $this->fmt_response(ReturnCode::para_miss,'登录方式错误');
        }
        if(empty($device)){
            return $this->fmt_response(ReturnCode::para_miss,'登录设备错误');
        }
        if(empty($version)){
            return $this->fmt_response(ReturnCode::para_miss,'版本号错误');
        }
        if($type=="ap_code"){
            $code = $this->request->input("code", NULL);
            if(empty($code)){
                return $this->fmt_response(ReturnCode::para_miss,'授权code不能为空');
            }
            $login_data = [
                'type' => $type,
                'code' => $code,
                'device' => $device,
                'version' => $version
            ];
        }

        $data = $this->loginServ->doLogin($login_data);
        if($data['status'] !== 1){
            return $this->fmt_response(ReturnCode::para_error,$data['msg']);
        }
        return $this->fmt_response(ReturnCode::deal_succ,'登陆成功',$data['data']);
    }
    #获取系统配置
    #[RequestMapping(path: "confs", methods: "post")]
    public function confs(){
        $key = $this->request->input("key", NULL);
        if(empty($key)){
            return $this->fmt_response(ReturnCode::para_miss,'key错误');
        }
        //医院
        if($key=="hospital"){
            $key_words = $this->request->input("key_words", "");
            $data = Common::getHospitalByName($key_words);
        //科室
        }elseif($key=="office"){
            $data = Common::getOfficeConf();
        //其他
        }else{
            $data = Common::getSystemConf($key);
        }
        return $this->fmt_response(ReturnCode::deal_succ,'获取成功',$data);
    }
    #点击统计
    #[RequestMapping(path: "hits", methods: "post")]
    public function hits(){
        $token = $this->request->input('token', NULL);
        if(!empty($token)){
            $is_login = $this->auth->guard('app')->check($token);
            if ($is_login) {
                $payload = $this->auth->guard('app')->getPayload($token);
                $user_id = $payload['uid'];
            }else{
                $user_id = Common::getUserIp();
            }
        }else{
            $user_id = Common::getUserIp();
        }
        $tag = $this->request->input("tag", NULL);
        $device = $this->request->input("device", NULL);
        $id = $this->request->input("id", NULL);
        $data = $this->request->input("data", NULL);
        if(empty($tag)){
            return $this->fmt_response(ReturnCode::para_miss,'统计标签错误');
        }
        if(empty($device)){
            return $this->fmt_response(ReturnCode::para_miss,'统计设备错误');
        }
        $static_params = [
            'device'=>$device,
            'tag'=>$tag,
            'user_id'=>$user_id,
            'id'=>$id,
            'data'=>$data,
            'save_type'=>'norepeat'
        ];
        $res = $this->staServ->doSave($static_params);
        return $this->fmt_response(ReturnCode::deal_succ,'成功');
    }
    #获取七牛配置
    #[RequestMapping(path: "uploadtoken", methods: "post")]
    public function uploadtoken()
    {
        $mod = $this->request->input('mod', NULL);
        if(empty($mod)){
            return $this->fmt_response(ReturnCode::para_miss,'模块错误');
        }
        $accessKey = env('QINIU_ACCESS_KEY');
        $secretKey = env('QINIU_SECRET_KEY');
        $bucket = env('QINIU_BUCKET');
        $domain = env('QINBIU_DOMAIN');
        $adapter = new QiniuAdapter($accessKey, $secretKey, $bucket, $domain);
        $token = $adapter->getUploadToken();
        $return_data = [
            'host' => "https://7img.512xhq.com/",
            'bucket' => "xiaohuquan",
            'path' => "supaiban/".$mod."/".date("Ymd")."/",
            'token' => $token
        ];
        return $this->fmt_response(ReturnCode::deal_succ,'成功',$return_data);
    }
    #获取文案
    #[Middleware(AuthMiddleware::class)]
    #[RequestMapping(path: "copyrights", methods: "post")]
    public function copyrights()
    {
        $type = $this->request->input('type', NULL);
        $office_id = $this->request->input('office_id', NULL);
        $user_id = $this->request->getAttribute('user_id');
        $begin_date = $this->request->input('begin_date', NULL);
        $end_date = $this->request->input('end_date', NULL);
        if(empty($type)){
            return $this->fmt_response(ReturnCode::para_miss,'类型错误');
        }
        $params = [
            'office_id'=>$office_id,
            'user_id'=>$user_id,
            'begin_date'=>$begin_date,
            'end_date'=>$end_date,
        ];
        $res = $this->cpyServ->shareCopyright($type,$params);
        return $this->fmt_response(ReturnCode::deal_succ,'成功',$res);
    }
}